{% extends 'base.html' %}

{% block mbx %}
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-sm-4">
            <h2>用户展示</h2>
            <ol class="breadcrumb">
                <li>
                    <a hreaf="{% url 'index' %}">首页</a>
                </li>
                <li>
                    <a href="">用户管理</a>
                </li>
                <li>
                    <a href="">用户展示</a>
                </li>
            </ol>
        </div>
    </div>
{% endblock %}

{% block body %}
    <div class="col-lg-12">
        {#        <div class="ibox float-e-margins">#}
        <div class="ibox-title">
            <h5>用户展示 </h5>
        </div>
        <div class="ibox-content">
        {% if perms.users.add_user %}
            <a class="btn btn-primary" href="{% url 'user_add' %}">添加用户</a>
        {% endif %}
            <table class="table table-striped">
                <thead>
                <tr>
                    <th class="text-center">用户名</th>
                    <th class="text-center">邮箱</th>
                    <th class="text-center">微信</th>
                    <th class="text-center">中文名</th>
                    <th class="text-center">电话</th>
                    <th class="text-center">激活状态</th>
                    <th class="text-center">操作</th>
                </tr>
                </thead>
                <tbody>
                {% for one in object_list %}
                    <tr>
                        <td class="text-center">{{ one.username }}</td>
                        <td class="text-center">{{ one.email }}</td>
                        <td class="text-center">{{ one.profile.wechat }}</td>
                        <td class="text-center">{{ one.profile.name_cn }}</td>
                        <td class="text-center">{{ one.profile.phone }}</td>
                        <td class="text-center">
                            {% if one.is_active == 1 %}
                                <i class="fa fa-circle text-navy"></i>
                            {% else %}
                                <i class="fa fa-circle text-danger"></i>
                            {% endif %}
                        </td>
                        {#                        <td class="text-center"><i class="fa fa-circle text-danger"></i></td>#}
                        <td class="text-center">
                            <a type="button" class="btn btn-primary btn-sm"
                               href="{% url 'user_update' %}?id={{ one.id }}">更新</a>
                            <button type="button" class="btn btn-danger btn-sm" onclick="user_delete({{ one.id }})">删除
                            </button>
                            {% if one.is_active == 0 %}
                                <button type="button" class="btn btn-info btn-sm" onclick="user_status({{ one.id }})">
                                    启用
                                </button>
                            {% else %}
                                <button type="button" class="btn btn-warning btn-sm" onclick="user_status({{ one.id }})">
                                    禁用
                                </button>
                            {% endif %}
                        {% if perms.users.user_set_perm %}
                            <a href="{% url 'user_set_perm' %}?id={{ one.id }}" class="btn btn-success btn-sm">权限</a>
                        {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            <center>
                <div class="btn-group">
                    {% if page_obj.has_previous %}
                        <a type="button" class="btn btn-white"
                           href="{% url 'user_list' %}?page=1">最前页</a>
                        <a type="button" class="btn btn-white"
                           href="{% url 'user_list' %}?page={{ page_obj.previous_page_number }}"><i
                                class="fa fa-chevron-left"></i></a>
                    {% else %}
                        <a type="button" class="btn btn-white disabled"
                           href="{% url 'user_list' %}?page=1">最前页</a>
                        <a type="button" class="btn btn-white disabled"
                           href="{% url 'user_list' %}"><i class="fa fa-chevron-left"></i></a>
                    {% endif %}
                    {#{% for page in paginator.page_range %}#}
                    {% for page in page_range %}
                        {% if page_obj.number == page %}
                            <a class="btn btn-white active"
                               href="{% url 'user_list' %}?page={{ page }}">{{ page }}</a>
                        {% else %}
                            <a class="btn btn-white"
                               href="{% url 'user_list' %}?page={{ page }}">{{ page }}</a>
                        {% endif %}
                    {% endfor %}
                    {% if page_obj.has_next %}
                        <a type="button" class="btn btn-white"
                           href="{% url 'user_list' %}?page={{ page_obj.next_page_number }}"><i
                                class="fa fa-chevron-right"></i></a>
                        <a type="button" class="btn btn-white"
                           href="{% url 'user_list' %}?page={{ paginator.num_pages }}">最后页</a>
                    {% else %}
                        <a type="button" class="btn btn-white disabled"
                           href="{% url 'user_list' %}?page={{ paginator.num_pages }}"><i
                                class="fa fa-chevron-right"></i></a>
                        <a type="button" class="btn btn-white disabled"
                           href="{% url 'user_list' %}?page={{ paginator.num_pages }}">最后页</a>
                    {% endif %}
                </div>

            </center>
        </div>
    </div>
    </div>
{% endblock %}
{% block load_js %}
    <script>
        function user_delete(id) {
            $.get("{% url 'user_delete' %}", {id: id}, function (res) {
                if (res.status == 0) {
                    swal({
                        title: res.msg,
                        type: 'success',
                        confirmButtonText: "知道了"
                    }, function () {
                        window.location.reload()
                        {#window.location.href = "{% url 'user_list' %}";#}
                    });
                } else {
                    swal({
                        title: res.msg,
                        type: 'error',
                        confirmButtonText: "知道了"
                    });
                }

            })
        }

        function user_status(id) {
            $.get("{% url 'user_status' %}", {id: id}, function (res) {
                if (res.status == 0) {
                    swal({
                        title: res.msg,
                        type: 'success',
                        confirmButtonText: "知道了"
                    }, function () {
                        window.location.reload()
                        {#window.location.href = "{% url 'user_list' %}";#}
                    });
                } else {
                    swal({
                        title: res.msg,
                        type: 'error',
                        confirmButtonText: "知道了"
                    });
                }

            })
        }
    </script>
{% endblock %}